﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;

namespace WebToolService
{
    public class CultureHelper
    {
        private static Dictionary<string, string> LanguageDictionary = new Dictionary<string, string>()
            {
                { "zh", "zh-CN"},
            };

        public static void SetCurrentCulture(CultureInfo culture)
        {
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }

        public static void SetCurrentCulture(string[] userLanguages)
        {
            string lang = GetLang(userLanguages);
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(lang);
            SetCurrentCulture(culture);
        }
        private static string GetLang(string[] userLanguages)
        {
            string lang = "en";
            if (userLanguages != null && userLanguages.Length != 0)
            {
                foreach (string s in userLanguages)
                {
                    string shortLang = s.Substring(0, 2);
                    if (LanguageDictionary.ContainsKey(shortLang))
                    {
                        lang = LanguageDictionary[shortLang];
                        break;
                    }
                }
            }
            return lang;
        }
    }
}
